﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Abp.AutoMapper;
using FwProject.Com;
using FwProject.Com.Dto;
using FwProject.Controllers;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using QRCoder;

namespace FwProject.Web.Shop.Controllers
{
    public class PrintController : FwProjectControllerBase
    {
        private readonly IShopOrderDetailsAppService _shopOrderDetailsAppService;

        public PrintController(IShopOrderDetailsAppService shopOrderDetailsAppService)
        {
            _shopOrderDetailsAppService = shopOrderDetailsAppService;
        }

        public async Task<IActionResult> OrdersPrint(Guid orderId,string orderNumber,DateTime orderTime,string userName,string mobile,string address)
        {
            ViewBag.orderId = orderId;
            ViewBag.OrderNumber = orderNumber;
            ViewBag.OrderTime = orderTime;
            ViewBag.UserName = userName;
            ViewBag.Mobile = mobile;
            ViewBag.Address = address;
            var model = await _shopOrderDetailsAppService.GetShopOrderDetailsByOrderIdAsync(orderId);
            return View(model);
        }
        public async Task<IActionResult> TicketPrint(Guid orderId, string orderNumber, DateTime orderTime, string userName, string mobile, string address)
        {
            ViewBag.orderId = orderId;
            ViewBag.OrderNumber = orderNumber;
            ViewBag.OrderTime = orderTime;
            ViewBag.UserName = userName;
            ViewBag.Mobile = mobile;
            ViewBag.Address = address;
            var model = await _shopOrderDetailsAppService.GetShopOrderDetailsByOrderIdAsync(orderId);
            return View(model);
        }

        public ActionResult OrderQr(string orderNumber)
        {
            string strCode = $"{orderNumber}";
            QRCodeGenerator qrGenerator = new QRCodeGenerator();

            QRCodeData qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.H);

            QRCode qrcode = new QRCode(qrCodeData);
            // qrcode.GetGraphic 方法可参考最下发“补充说明”
            Bitmap qrCodeImage = qrcode.GetGraphic(5, Color.Black, Color.White, null, 15, 6, false);
            MemoryStream ms = new MemoryStream();
            qrCodeImage.Save(ms, ImageFormat.Jpeg);
            return File(ms.ToArray(), "image/Jpeg");
        }


    }
}